package action.auth;

import org.apache.struts2.ServletActionContext;

import service.interf.security.IGroupMenu;

import com.opensymphony.xwork2.ModelDriven;

import dao.pojo.GroupMenu;
import action.common.MyBaseAction;

public class GroupMenuAction extends MyBaseAction<GroupMenu> implements ModelDriven<GroupMenu>{
	private IGroupMenu groupMenuImpl;
	

	public IGroupMenu getGroupMenuImpl() {
		return groupMenuImpl;
	}

	public void setGroupMenuImpl(IGroupMenu groupMenuImpl) {
		this.groupMenuImpl = groupMenuImpl;
	}

	public GroupMenuAction(){
		super(GroupMenu.class);
		validRules.put("group.groupId", "empty");
		validRules.put("ids","empty");
	}

	public GroupMenu getModel() {
		// TODO Auto-generated method stub
		return model;
	}
	
	//添加groupMenu
	public void addGroupMenu(){
		String r = "";
		if(!validMe("group.groupId")){
			return;
		}
		try {
			r = groupMenuImpl.addGroupMenu(model.getGroup().getGroupId(), model.getIds());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}
	
	//删除groupMenu
	public void removeGroupMenu(){
		String r = "";
		if(!validMe("group.groupId,ids")){
			return;
		}
		try{
			r = groupMenuImpl.removeGroupMenu(model.getGroup().getGroupId(), model.getIds());
		}catch(Exception e){
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), r);
	}
	//根据groupId获取该group的所有menu
	public void getGroupMenu(){
		String r = "";
		if(!validMe("group.groupId")){
			return;
		}
		try {
			r = groupMenuImpl.getMenuByGroupId(model.getGroup().getGroupId());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), r);
	}

	// 根据菜单Id获取该菜单所属的组
	public void getGroupByMenuId() {
		String r = "";
		try{
			r = groupMenuImpl.findByProperty("GroupMenu", "group.groupId(groupId),group.groupName(groupName),group.remarks(remarks)", "json", true, new String[]{"menu.menuId"}, new Object[]{model.getMenu().getMenuId()}, null, null, 0, 0);
		}catch(Exception e){
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), r);
	}
	//根据菜单id和组ids添加groupMenu
	public void addGroupMenuByMenuIdGroupIds(){
		String r = "";
		if(!validMe("menu.menuId")){
			return;
		}
		try {
			r = groupMenuImpl.addMenuGroup(model.getMenu().getMenuId(), model.getIds());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}
}
